#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# This file contains useful functions for fabric development.
# Modify the following environment variables as desired.
# The remaining variables should not need to be change.
# LFID        : your Linux Foundation ID
# REPO        : your repository name
export LFID=YOUR-LINUX-FOUNDATION-ID
export REPO=fabric-ca

export PROJECT=github.com/hyperledger
export GERRIT=gerrit.hyperledger.org
export GERRIT_ADDR=$GERRIT:29418

# Clone the repo
function clone {
  initvars
  if [ -d $LFID ]; then
     echo "LFID environment variable must be set to your Linux Foundation ID"
     return 1
  fi
  if [ -d $REPO_DIR ]; then
     echo "repo already exists at $REPO_DIR"
     return 1
  fi
  if [ ! -d $PROJECT_DIR ]; then
     mkdir -p $PROJECT_DIR
  fi
  cd $PROJECT_DIR
  echo ">>> Cloning directory at $REPO_DIR ..."
  git clone ssh://${LFID}@${GERRIT_ADDR}/${REPO}
  cdr
  git config --global gitreview.username $LFID
  git config --global gitreview.remote origin
  gitdir=$(git rev-parse --git-dir)
  scp -p -P 29418 ${LFID}@${GERRIT}:hooks/commit-msg ${gitdir}/hooks/  > /dev/null
  echo ">>> Completed clone of directory at $REPO_DIR"
}

# Clone the repo to a new directory, typically for verification
function clonedir {
  OLD_GOPATH=$GOPATH
  if [ $# -ne 1 ]; then
     echo "Usage: clonedir <dir>"
     return 1
  fi
  if [ -d $1 ]; then
     echo "directory $1 already exists"
     return 1
  fi
  mkdir -p $1
  cd $1
  export GOPATH=`pwd`
  clone
  echo "WARNING: Your GOPATH was changed from ${OLD_GOPATH} to ${GOPATH}.  Be sure to change it back when done."
}

# Create a new branch
function branch {
  initvars
  if [ $# -ne 1 ]; then
     echo "Usage: branch <branch-name>"
     return 1
  fi
  cdr
  if [ $? -ne 0 ]; then
    return $?
  fi
  git checkout master
  if [ $? -ne 0 ]; then
    return $?
  fi
  git checkout -b $1
  return $?
}

# push changes to repo
function push {
 cdr
 if [ $? -ne 0 ]; then
   return 1
 fi
 echo "Running make before pushing ..."
 make unit-tests docs
 if [ $? -ne 0 ]; then
   echo "Not pushing because make failed"
   return 1
 fi
 git push origin HEAD:refs/for/master
}

# Rebase your current branch on the latest from master
function rebase {
   cdr
   CUR_BRANCH=`git rev-parse --abbrev-ref HEAD`
   if [ "${CUR_BRANCH}" = "master" ]; then
      echo "Can't rebase from master branch"
      return 1
   fi
   git checkout master
   if [ $? -ne 0 ]; then
      echo "Failed to checkout out master"
      return 1
   fi
   echo "Updating master ..."
   git pull
   if [ $? -ne 0 ]; then
      echo "Failed to pull from master"
      return 1
   fi
   git checkout $CUR_BRANCH
   if [ $? -ne 0 ]; then
      echo "Failed to switch back to branch $CUR_BRANCH"
      return 1
   fi
   git rebase master
   if [ $? -ne 0 ]; then
      echo "Failed to rebase branch $CUR_BRANCH against master"
      return 1
   fi
}

# setrepo
function setrepo {
  if [ $# -ne 1 ]; then
     echo "Usage: setrepo <repo>"
     echo "Examples:"
     echo "   setrepo fabric-ca"
     echo "   setrepo fabric"
     return 1
  fi
  export REPO=$1
  initvars
  if [ ! -d $REPO_DIR ]; then
     clone
  else
     cdr
  fi
}

function verify {
   rm -rf $HOME/verify
   clonedir $HOME/verify
}

# Change to the repo directory
function cdr {
  initvars
  if [ ! -d $REPO_DIR ]; then
     echo "repo does not exist at $REPO_DIR"
     return 1
  fi
  cd $REPO_DIR
  return 0
}

# Initialize variables appropriately
function initvars {
   export PROJECT_DIR=$GOPATH/src/$PROJECT
   export REPO_DIR=$PROJECT_DIR/$REPO
}

# Generate the coverage report
function gencov {
   cdr
   echo "Generating coverage report ..."
   go get github.com/axw/gocov/gocov
   go get -u gopkg.in/matm/v1/gocov-html
   gocov test `go list ./... | grep -Ev '/vendor/|/api|/dbutil|/ldap'` | gocov-html > /tmp/coverage.html
   echo "View the coverage report by pasting the following URL in your browser: file:///tmp/coverage.html"
}
